package novel.mzx.com.activity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gyf.immersionbar.ImmersionBar;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import novel.mzx.com.R;
import novel.mzx.com.base.BaseActivity;
import novel.mzx.com.bean.CodeBean;
import novel.mzx.com.http.Api;
import novel.mzx.com.http.SpDataUtils;
import novel.mzx.com.utils.JSONUtils;
import novel.mzx.com.utils.ToastUtils;

/**
 * 添加评论页面：
 *
 * */

public class AddCommentActivity extends BaseActivity {


    @BindView(R.id.et_01)
    EditText et_content;

    @BindView(R.id.btn_login)
    Button btn_login;

    // 返回按钮
    @BindView(R.id.iv_back)
    ImageView iv_back;

    String id;
    @Override
    protected int getResourceId() {
        return R.layout.activity_add_comment;
    }

    @Override
    protected void initView() {
        super.initView();
        // android 4.4以上沉浸式以及bar的管理完成一些控件的初始化
        ImmersionBar.with(this).titleBar(R.id.toolbar).keyboardEnable(true).init();
        ImmersionBar.with(this).keyboardEnable(false).statusBarDarkFont(true, 0.2f).navigationBarColor(R.color.btn3).init();

        // 返回按钮的逻辑，关闭当前Activity
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        id = getIntent().getStringExtra("id");

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 构建日期格式字段
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                // 自定义日期格式字段信息
                String format = df.format(new Date());
                OkGo.<String>post(Api.mainHost+Api.addCommentInfoUrl)
                        .params("commentAccount", SpDataUtils.getAccount())
                        .params("commentName", SpDataUtils.getUserName())
                        .params("commentContent", et_content.getText().toString().trim())
                        .params("commentTime", format)
                        .params("commentCircleId", id)
                        .params("commentLogo", SpDataUtils.getUserLogo())
                        .params("commentState","同意")
                        .execute(new StringCallback() {
                            // 评论成功逻辑   返回分享Activity
                            @Override
                            public void onSuccess(Response<String> response) {
                                Log.e("评论",response.body());

                                CodeBean codeBean = JSONUtils.parserObject(response.body(),CodeBean.class);
                                int code = codeBean.getResultCode();
                                if(code == 200){
                                    ToastUtils.showToast(AddCommentActivity.this,"评论成功!");
                                    Intent intent = new Intent();
                                    intent.setAction("freshCircle");
                                    sendBroadcast(intent);

                                    Intent intent2 = new Intent();
                                    intent2.setAction("refreshComment");
                                    sendBroadcast(intent2);
                                    finish();

                                }else {

                                }
                            }

                            @Override
                            public void onError(Response<String> response) {
                                super.onError(response);

                            }
                        });
            }
        });
    }


}
